﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc @Copy Right 2019
// 文件： CUserInfoRepository.cs
// 项目名称：斑马车务
// 创建时间：2019-09-17
// 负责人：lsc
// ===================================================================
using System;
using System.Text;
using System.Collections.Generic;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Models;
using Dapper;
using Dapper.Extensions;
using YouWei.Utility;

namespace YouWei.Infrastructure.Repository
{
	/// <summary>
	///C端用户表 仓储层
	/// </summary>	
	public class BUserInfoRepository:BaseRepository<BUserInfo>
	{		
    
        #region 构造

        public BUserInfoRepository() : base(){}

        public BUserInfoRepository(string connectionString) : base(connectionString){}

        public BUserInfoRepository(DatabaseContext dbContext) : base(dbContext){}

        #endregion
        /// <summary>
        /// 查询用户列表
        /// 此方法仅用于管理员
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Page<BUserInfoPageModel> Pages(int pageIndex, int pageSize, BUserSearchModel model)
        {
            StringBuilder sb = new StringBuilder("SELECT T1.*,b.cName,b.pName,b.locationName FROM [BUserInfo] T1 WITH(NOLOCK) ");
            //注：base_locationAll表里的locationId是主键，userinfo表里的location是地区，意义不一样。所以这里拿Hcity=locationid   liucx
            sb.Append(" Left Join BaseLocation_All b WITH(NOLOCK) ON T1.DCity= b.locationId");
            sb.Append(" WHERE T1.IsActvie=1");
            var dparams = new DynamicParameters();
            if (model != null)
            {
                AddParams(model, sb, dparams);
            }

            sb.Append(" ORDER BY T1.UserID DESC");
            return dbContext.Connection.Page<BUserInfoPageModel>(pageIndex, pageSize, sb.ToString(), dparams);
        }
        /// <summary>
        /// 构建查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="sb"></param>
        /// <param name="dparams"></param>
        private void AddParams(BUserSearchModel model, StringBuilder sb, DynamicParameters dparams)
        {
            if (!string.IsNullOrEmpty(model.Mobile))
            {
                sb.Append(" AND T1.Mobile=@Mobile");
                dparams.Add("Mobile", model.Mobile, System.Data.DbType.String);
            }

            if (model.HCity > 0)
            {
                sb.Append(" AND T1.DCity=@DCity");
                dparams.Add("DCity", model.HCity, System.Data.DbType.Int32);
            }
            else if (model.HProince > 0)
            {
                sb.Append(" AND T1.DProince=@DProince");
                dparams.Add("DProince", model.HProince, System.Data.DbType.Int32);
            }

            if (!string.IsNullOrEmpty(model.BCreateDate))
            {
                sb.Append(" AND T1.CreateDate>@BCreateDate");
                dparams.Add("BCreateDate", EConvert.ToDateTime(model.BCreateDate).Date, System.Data.DbType.DateTime);
            }

            if (!string.IsNullOrEmpty(model.ECreateDate))
            {
                sb.Append(" AND T1.CreateDate<@ECreateDate");
                dparams.Add("ECreateDate", EConvert.ToDateTime(model.ECreateDate).Date.AddDays(1), System.Data.DbType.DateTime);
            }
        }
    }

}